/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trim.gui.testing.models;

import java.lang.reflect.Field;
import javax.swing.table.AbstractTableModel;

/**
 * Модель редактирования свойств объекта
 * @author Дмитрий
 */
public class PropertiesTableModel extends AbstractTableModel
{

  Class clazz;
  Object o;
  KeyValue[] values=new KeyValue[0];
  //TableModel tableModel;
  boolean editable=true;

  public PropertiesTableModel()
  {
  }

  public PropertiesTableModel(boolean editable)
  {
    this.editable=editable;
  }

  public void setObject(Object o)
  {
    if(o==null)
    {
      values=new KeyValue[0];
      return;
    }
    try
    {
      this.clazz=o.getClass();
      this.o=o;
      Field[] fs=clazz.getDeclaredFields();
      values=new KeyValue[fs.length];

      for(int i=0;i<fs.length;i++)
      {
        //Object q=clazz.getMethod(makeGetter(fs[i].getName()),new Class[0]).invoke(o,new Object[0]);
        fs[i].setAccessible(true);
        Object q=fs[i].get(o);
        values[i]=new KeyValue(fs[i].getName(),q,fs[i]);

      }
    }catch(Exception ex)
    {
      ex.printStackTrace();
    }
    fireTableStructureChanged();
  //fireTableDataChanged();
  }

  @Override
  public String getColumnName(int column)
  {
    switch(column)
    {
      case 0:
        return "Name";
      case 1:
        return "Value";
    }
    return super.getColumnName(column);
  }

  public int getRowCount()
  {
    return values.length;
  }

  public int getColumnCount()
  {
    return 2;
  }

  public Object getValueAt(int rowIndex,int columnIndex)
  {

    switch(columnIndex)
    {
      case 0:
        return values[rowIndex].getKey();
      case 1:
        return values[rowIndex].getValue();
    }
    return null;
  }

  @Override
  public void setValueAt(Object aValue,int rowIndex,int columnIndex)
  {
    super.setValueAt(aValue,rowIndex,columnIndex);
    values[rowIndex].setValue(aValue);
    try
    {
      values[rowIndex].getField().set(o,aValue);
    }catch(Exception ex)
    {
      ex.printStackTrace();
    }
    fireTableCellUpdated(rowIndex,columnIndex);
  }

  @Override
  public Class<?> getColumnClass(int columnIndex)
  {
    if(columnIndex==1)
      return Integer.class;
    else
      return String.class;
  }

  @Override
  public boolean isCellEditable(int rowIndex,int columnIndex)
  {
    return editable&&columnIndex==1;
  }

  /**
   * Класс предназначенный для ранения пар <ключ,значение>
   */
  class KeyValue
  {

    String key;
    Object value;
    Field field;

    public KeyValue(String key,Object value,Field filed)
    {
      this.key=key;
      this.value=value;
      this.field=filed;
    }

    public String getKey()
    {
      return key;
    }

    public void setKey(String key)
    {
      this.key=key;
    }

    public Object getValue()
    {
      return value;
    }

    public void setValue(Object value)
    {
      this.value=value;
    }

    public Field getField()
    {
      return field;
    }
  }
}
